package com.iw.sms.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alvin.sms.model.SendSmsVO;
import com.iw.sms.entity.Sms;
import com.iw.sms.entity.SmsType;
import com.iw.sms.service.SmsService;

@Component
@Aspect
public class SendSmsAspect {
	@Autowired
	private SmsService smsService;

	/**
	 * 系统发送短信的时候拦截功能
	 * 
	 * @param jp
	 */
	@After("execution(* com.alvin.sms.service.SmsService.sendSms(..))")
	public void doBeforeInServiceLayer(JoinPoint jp) {
		try {
			Object[] ob = jp.getArgs();
			if (ob != null && ob.length > 0) {
				if (ob[0] instanceof SendSmsVO) {
					SendSmsVO smsVO = (SendSmsVO) ob[0];
					if (smsVO.getStime() != null) {//没了发送时间，就表示这条短信没有被发送，所以就不用保存它了
						Sms sms = new Sms();
						sms.setContent(smsVO.getContent());
						sms.setExt(smsVO.getExt());
						sms.setMobile(smsVO.getMobile());
						sms.setRrid(smsVO.getRrid());
						sms.setStime(smsVO.getStime());
						sms.setType(SmsType.S);
						smsService.save(sms);
					}
				}
			}
		} catch (Exception e) {
		}

	}

}
